package com.chzsyh.evwer

import android.app.Application
import android.util.Log
import com.fuusy.project.di.moduleCertification
import com.owbtgad.common.TestTaskUtils
import com.owbtgad.common.utils.InsertHelp
import com.owbtgad.common.utils.SystemInfoUtils
import com.owbtgad.common.utils.UrlHelp
import com.shuyihui.rxhttp.EasyHttp
import com.xj.anchortask.library.OnProjectExecuteListener
import com.xj.anchortask.library.monitor.OnGetMonitorRecordCallback
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.startKoin
import org.koin.core.logger.Level

private const val TAG = "SampleApp"
/**
 * 创建日期 2020/9/24
 * 描述：
 */
class SampleApp : Application() {
    private val appModule = arrayListOf(
         moduleCertification
    )

    private lateinit var mContext:Application
    override fun onCreate() {
        super.onCreate()
        mContext = this
        UrlHelp.setBsseUrl(BuildConfig.baseUrl)
        startKoin {
            androidLogger(Level.NONE)
            androidContext(this@SampleApp)
            modules(appModule)
        }

        EasyHttp.init(this)
        EasyHttp.getInstance().debug("RxEasyHttp",BuildConfig.DEBUG)
        InsertHelp.setAppVersion(BuildConfig.VERSION_NAME)
        InsertHelp.setScreenWidth(
            SystemInfoUtils.getScreenWidth(
                mContext
            )
        )
        InsertHelp.setScreenHeight(
            SystemInfoUtils.getScreenHeight(
                mContext
            )
        )
        InsertHelp.setOsVersion(
            SystemInfoUtils.getOSVersionCode()
                .toString()
        )
        InsertHelp.setMfrs(SystemInfoUtils.getManufacturerName())
        InsertHelp.setModel(SystemInfoUtils.getModelName())

        InsertHelp.setAppCode(BuildConfig.appCode)
        //******************************************************
//        ApkTool.getPackages(mContext)

        InsertHelp.setAppChannel("52c38784bc33f155201864df192dd1c8")
        InsertHelp.setAppChannel("1")
        TestTaskUtils.executeTask(this, object : OnProjectExecuteListener {
            val sb = StringBuffer()
            override fun onProjectStart() {
                if (sb.isNotEmpty()) {
                    sb.delete(0, sb.length)
                }

            }

            override fun onTaskFinish(taskName: String) {
                sb.append("task $taskName execute finish \n")
            }

            override fun onProjectFinish() {

            }

        }, object : OnGetMonitorRecordCallback {


            override fun onGetTaskExecuteRecord(result: Map<String?, Long?>?) {
                result?.entries?.iterator()?.forEach {
                    Log.d(TAG, "onGetTaskExecuteRecord: ${it.key}执行耗时"+it.value+" 毫秒")
                }
            }

            override fun onGetProjectExecuteTime(costTime: Long) {
//                sb2.append("总共执行耗时").append(costTime).append("毫秒\n")
                Log.d(TAG, "onGetProjectExecuteTime: $costTime")
            }

        })
        Log.d(TAG, "onCreate: 22222222")
    }

}